﻿/*
 * @author: wizardc
 */

using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

namespace Dou.Utils
{
    /// <summary>
    /// 不安全工具类
    /// </summary>
    public static unsafe class UnsafeUtil
    {
        /// <summary>
        /// 分配指定对象并返回指针
        /// </summary>
        public static T* Malloc<T>(Allocator allocator = Allocator.TempJob) where T : unmanaged
        {
            return Malloc<T>(1, allocator);
        }
        
        /// <summary>
        /// 分配指定对象的数组并返回指针
        /// </summary>
        public static T* Malloc<T>(uint count, Allocator allocator = Allocator.TempJob) where T : unmanaged
        {
            return (T*) UnsafeUtility.Malloc(UnsafeUtility.SizeOf<T>() * count, UnsafeUtility.AlignOf<T>(), allocator);
        }
        
        /// <summary>
        /// 释放指针
        /// </summary>
        public static void FreePtr(void* ptr, Allocator allocator = Allocator.TempJob)
        {
            UnsafeUtility.Free(ptr, allocator);
        }
    }
}
